package com.example.springbootnetty.socket;

import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.OnConnect;
import com.corundumstudio.socketio.annotation.OnDisconnect;
import com.corundumstudio.socketio.annotation.OnEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class ChatHandler {
    private final SocketIOServer server;

    private Map<String, Victor> userPosition = new HashMap<>();

    @Autowired
    public ChatHandler(SocketIOServer socketIOServer) {
        this.server = socketIOServer;
//        new Thread(() -> {
//            System.out.println("发送线程启动");
//            while (true) {
//                try {
//                    Thread.sleep(100);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                server.getBroadcastOperations().sendEvent("gameMessage", getUserPosition());
//            }
//
//        }).start();
    }

    @OnEvent(value = "chatevent")
    public void onEvent(SocketIOClient client, AckRequest ackRequest, ChatObject data) {
        ackRequest.sendAckData("收到了chatevent");
        server.getBroadcastOperations().sendEvent("chatevent", data);
    }

    //获取游戏消息
    @OnEvent(value = "gameMessage")
    public void startGame(SocketIOClient client, AckRequest ackRequest) {

    }

    @OnEvent(value = "updateUserIndex")
    public void updateUserIndex(SocketIOClient client, AckRequest ackRequest, Victor victor) {
        userPosition.put(client.getSessionId().toString(), victor);
    }

    @OnDisconnect
    public void disConnectUser(SocketIOClient client) {
        System.out.println("丢失链接了");
        userPosition.remove(client.getSessionId().toString());
    }

    public Collection<Victor> getUserPosition() {
        return userPosition.values();
    }
    //    @OnConnect
//    public void onconnect(SocketIOClient client){
//        System.out.println("lianjie");
////        server.getBroadcastOperations().sendEvent("chatevent",data);
//    }

//    @OnEvent(value = "connect")
//    public void onconnect(SocketIOClient client){
//        System.out.println("lianjie111");
//        server.getBroadcastOperations().sendEvent("connect",new ChatObject("21","123"));
//    }
}
